package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealOperateException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {
    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList = setmealMapper.getLikeSetmeal(setmealPageQueryDTO);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @Transactional
    @Cacheable(cacheNames = "SETMEAL_BY_CATEGORY_ID", key = "#setmeal.categoryId")
    public void addSetmeal(SetmealDTO setmeal) {
        Integer setmealName = setmealMapper.getSetmealCount(setmeal.getName());
        if (setmealName > 0) {
            throw new SetmealOperateException(MessageConstant.SETMEAL_FAIL_NAME);
        }
        Setmeal meal = new Setmeal();
        BeanUtils.copyProperties(setmeal, meal);
        meal.setStatus(StatusConstant.DISABLE);
        setmealMapper.addSetmeal(meal);
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        setmealDishes.forEach(s -> {
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(s, setmealDish);
            setmealDish.setSetmealId(meal.getId());
            setmealDishMapper.addSetmealDish(setmealDish);
        });
    }

    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        setmealMapper.deleteSetmeal(ids);
        setmealDishMapper.delSetmealDish(ids);
    }

    @Override
    public SetmealVO echoSetmeal(Long id) {
        Setmeal setmeal = setmealMapper.getSetmeal(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDish = setmealDishMapper.getSetmealDish(id);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "SETMEAL_BY_CATEGORY_ID", key = "#setmeal.categoryId")
    public void updateSetmeal(SetmealDTO setmeal) {
        Integer setmealName = setmealMapper.getSetmealCounts(setmeal.getName(), setmeal.getId());
        if (setmealName > 0) {
            throw new SetmealOperateException(MessageConstant.SETMEAL_FAIL_NAME);
        }
        Setmeal meal = new Setmeal();
        BeanUtils.copyProperties(setmeal, meal);
        setmealMapper.updateSetmeal(meal);
        setmealDishMapper.delSetmealDish(Collections.singletonList(meal.getId()));
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        setmealDishes.forEach(s -> {
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(s, setmealDish);
            setmealDish.setSetmealId(meal.getId());
            setmealDishMapper.addSetmealDish(setmealDish);
        });
    }

    @Override
    @CacheEvict(cacheNames = "SETMEAL_BY_CATEGORY_ID", allEntries = true)
    public void updateStatus(Long id, Integer status) {
        Setmeal meal = new Setmeal();
        meal.setId(id);
        meal.setStatus(status);
        setmealMapper.updateSetmeal(meal);
    }
}
